package com.littlepay.kr4.paulin.dfeba77.adapter;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.littlepay.kr4.paulin.dfeba77.application.MyApplication;
import com.littlepay.kr4.paulin.dfeba77.config.SqliteConfig;
import com.littlepay.kr4.paulin.dfeba77.entity.Sticker;
import com.littlepay.kr4.paulin.dfeba77.utils.DateUtil;
import com.littlepay.kr4.paulin.dfeba77.utils.SqliteHelper;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by 77dfeba on 2017/6/11.
 * 用于设置表格的
 *      横纵轴的长度
 *      返回当月的消费entry
 */

public class LineChartAdapter {
    //月份不会变,
    private static int minMonth,maxMonth,currentMonth;

    public static int getMinMonth() {
        return minMonth;
    }

    public static int getMaxMonth() {
        return maxMonth;
    }

    public static int getCurrentMonth() {
        return currentMonth;
    }

    public LineChartAdapter(){
        //获取当前月份并且截取下来作为当前月份
        currentMonth = Integer.parseInt(DateUtil.getMonthDate().substring(6,7)) - 1;
        maxMonth = currentMonth + 1;
        if(currentMonth - 4 <= 0){
            minMonth = 0;
        }else {
            minMonth = currentMonth - 4;
        }
    }
    public void setXAxis(LineChart lineChart){
        XAxis xAxis = lineChart.getXAxis();
        //表格一个数据之间是0.8间隔。因此需要矫正底部坐标为显示整数值
        //否则在月份设定时会出现2.4截断2显示成3月从而不能正确对准线条正确数据
        xAxis.setAxisMinimum((float) minMonth - 0.4f);
        xAxis.setAxisMaximum((float)maxMonth + 0.4f);
    }

    public void setYAxis(LineChart lineChart,List<Float> costList){
        YAxis yAxis = lineChart.getAxisLeft();
        float minY = 0f;
        float maxY = 0f;
        for(int i = 0; i<costList.size(); i++){
            float c = costList.get(i);
            if (minY > c){
                minY = c;
            }
            if (maxY < c){
                maxY = c;
            }
        }
        //没有数据下的处理
        if(minY == 0f && maxY == 0f){
            minY = -100f;
            maxY = 300f;
        }
        //此效果下的数据显示最好
        yAxis.setAxisMinimum(minY - maxY);
        yAxis.setAxisMaximum(maxY * 2);
        yAxis.setEnabled(false);
    }

    public ArrayList<Float> getAllMonthCost(){
        String currentMonthDate = DateUtil.getNowDate();
        ArrayList<Float> allMonthTotalList = new ArrayList<>();
        //double allMonthTotal = 0;
        for (int i=minMonth;i<=currentMonth;i++){
            SqliteHelper sqliteHelper = new SqliteHelper(MyApplication.getInstance().getContext(), SqliteConfig.SQLITE_DATABASE_FILE_NAME, null, 1);
            //先获取月份为单位的stickerList然后再相加成每个月的总消费作为allMonthTotalList
            List<Sticker> currentMonthList;
            float totalCostMonth = 0;
            currentMonthList = sqliteHelper.queryDataListByMonth(currentMonthDate.substring(0,8));
            currentMonthDate = DateUtil.getLastMonth(currentMonthDate);
            for (int i1 = 0;i1 < currentMonthList.size();i1++){
                totalCostMonth += currentMonthList.get(i1).getCost();
            }
            allMonthTotalList.add(totalCostMonth);
        }
        return allMonthTotalList;
    }



}
